<template>
	<view class="filterContent">
		<filter-drawer
			:chooseData="choosed"
			:filterList="filterList"
			:comfireText="comfireText"
			:show="showFilter"
			:keyMap="keyMap"
			:isShowAll="false"
			:title="tabMap[tabIdx]"
			:orgin="type"
			ref="filter-drawer"
			@closed="handleClose"
			@submit="submitFilter"
			@chooseTag="getResultCount"
		></filter-drawer>
	</view>
</template>

<script>
import filterDrawer from './filter-drawer.vue';
import { timeLiness, duration, signType } from '@/apis//workOrder.js';

export default {
	components: { filterDrawer },
	props: {
		// 显示
		showFilter: {
			type: Boolean
		},
		tabIdx: {
			type: Number
		},
		csd: {
			type: Object,
			default: () => {
				return {};
			}
		}
	},
	created() {
		if (this.tabIdx == 0) {
			console.log(this.tabIdx);
			this.getMaintain();
			this.choosed = this.csd;
		}
	},

	data() {
		return {
			show: false,
			comfireText: '确定',
			choosed: {},
			filterList: [],
			chooseArr: [],
			tabMap: {
				0: '保养工单筛选',
				1: '维修工单筛选',
				2: '配件工单筛选'
			}
		};
	},
	methods: {
		async getMaintain() {
			const all = [timeLiness(), duration(), signType()];
			await Promise.all(all).then(res => {
				if (res) {
					let map = {
						0: {
							title: '及时性',
							key: 'timeliness'
						},
						1: {
							title: '任务时长',
							key: 'duration'
						},
						2: {
							title: '签到签退',
							key: 'signType'
						}
					};
					let arr = res.map((item, i) => {
						return {
							title: map[i].title,
							key: map[i].key,
							list: item.data
						};
					});
					this.filterList = [...arr];
					this.filterList.unshift({ title: '申请时间', key: 'date', list: [{ name: '近7天', value: 0 }, { name: '近30天', value: 1 }, { name: '近60天', value: 2 }] });
				}
			});
		},
		handleClose() {
			// console.log('点击关闭')
			// this.showFilter = false;
			// this.choosed={}
			// 重新赋值

			let obj = {};
			this.chooseArr.forEach(item => {
				if (obj[item.key]) {
					obj[item.key].push(item.value);
				} else {
					obj[item.key] = [];
					obj[item.key].push(item.value);
				}
			});
			this.choosed = obj;
			// console.log(obj,'obj的值');
			this.$refs['filter-drawer'].initData(obj);
			this.$emit('close');
		},
		// 获取数据
		getResultCount() {},
		// 确定按钮
		submitFilter(res) {
			console.log(res, 666);
			this.keyMap = res.keyMap;
			// console.log(this.keyMap, 222);
			this.allChecked = res.allChecked;
			this.choosed = res.choosed;
			// this.showFilter = false;
			this.setChooseArr();
			this.filterd();
			this.$emit('close');
		},
		filterd() {
			// console.log(this.choosed, 'this.choosed');
			this.$emit('filterd', {
				choosed: this.choosed,
				allChecked: this.allChecked
			});
		},
		// 渲染已经选择的标签
		setChooseArr() {
			let filterList = this.filterList;
			if (this.allChecked) {
				this.chooseArr = [];
				return;
			}
			let arr = [];

			for (let key in this.choosed) {
				let obj = this.filterList.find((item, i) => {
					return item.key === key;
				});
				// console.log(obj, 'obj的值set的值');
				if (obj) {
					obj.list.forEach(e => {
						if (this.choosed[key].indexOf(e.value) != -1) {
							arr.push({
								...e,
								key
							});
						}
					});
				}
			}

			this.chooseArr = arr;
		}
	}
};
</script>

<style lang="scss" scoped>
.filterContent {
}
</style>
